import 'package:bitcoin_mine/widgets/contracts_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/contract_controller.dart';
import '../l10n/app_localizations.dart';

class SortingPopup extends StatelessWidget {
  const SortingPopup({super.key});

  @override
  Widget build(BuildContext context) {
    final ContractController controller = Get.find();
    final l10n = AppLocalizations.of(context);

    return Obx(() {
      if (!controller.isSortingMenuVisible.value) {
        return const SizedBox.shrink();
      }

      // 获取屏幕尺寸
      final screenHeight = MediaQuery.of(context).size.height;
      final screenWidth = MediaQuery.of(context).size.width;

      // 获取按钮位置
      double popupTop = 200.h; // 默认位置
      double popupRight = 16.w;

      try {
        final RenderBox? buttonRenderBox =
            ContractsListWidget.sortingButtonKey.currentContext
                    ?.findRenderObject()
                as RenderBox?;
        if (buttonRenderBox != null) {
          final buttonPosition = buttonRenderBox.localToGlobal(Offset.zero);
          // 弹窗显示在按钮下方
          popupTop = buttonPosition.dy + buttonRenderBox.size.height + 8.h;
          popupRight =
              screenWidth - buttonPosition.dx - buttonRenderBox.size.width;
        }
      } catch (e) {
        // 如果获取位置失败，使用默认位置
        popupTop = 200.h;
        popupRight = 16.w;
      }

      // 确保弹窗不会超出屏幕底部
      final popupHeight = 120.h; // 预估弹窗高度
      final maxTop = screenHeight - popupHeight - 20.h;
      final finalTop = popupTop > maxTop ? maxTop : popupTop;

      return Positioned(
        top: finalTop,
        right: popupRight,
        child: Container(
          width: 200.w,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("assets/images/bg_sorting_popup.png"),
              fit: BoxFit.fill,
            ),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 选项列表
              Column(
                children: [
                  _buildSortOption(
                    l10n.vipContract,
                    controller.selectedSortType.value == 'VIP CONTRACT',
                    () => controller.selectSortType('VIP CONTRACT'),
                    l10n,
                  ),
                  _buildSortOption(
                    l10n.signInContract,
                    controller.selectedSortType.value == 'SIGN IN CONTRACT',
                    () => controller.selectSortType('SIGN IN CONTRACT'),
                    l10n,
                  ),
                  _buildSortOption(
                    l10n.advertisingContract,
                    controller.selectedSortType.value == 'ADVERTISING CONTRACT',
                    () => controller.selectSortType('ADVERTISING CONTRACT'),
                    l10n,
                  ),
                ],
              ),
            ],
          ),
        ),
      );
    });
  }

  Widget _buildSortOption(
    String title,
    bool isSelected,
    VoidCallback onTap,
    AppLocalizations l10n,
  ) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: double.infinity,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
        decoration: BoxDecoration(
          color:
              isSelected
                  ? const Color(0xFF00FFFF).withOpacity(0.2)
                  : Colors.transparent,
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
              child: Text(
                title,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: isSelected ? const Color(0xFF061125) : Colors.white,
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
